import java.io.*;
import java.net.*;
import java.util.List;

public class VKScraper {
	
	// Private fields
	private String Guid = null;

	// Constructor
	public VKScraper() {

		// TODO: Do important construction stuff here

	}

    // Properties
    public Boolean isLoggedIn() {
        
        return Guid != null;

    }

	// Public functions
	public Boolean login(String username, String password) {
		
        // TODO: Use one of the default logins if username and password are not set
        Guid = null; // Reset GUID
        Boolean error = false;

        HttpURLConnection request = null;

        try {
            
            // Make connection
            CookieManager cookieManager = new CookieManager(); // Create a cookie manager
            CookieHandler.setDefault(cookieManager); // Set default cookie manager
            URL url = new URL("http://login.vk.com/"); // Create url
            request = (HttpURLConnection) url.openConnection(); // Create new connection
            request.setRequestMethod("POST"); // Set method to post
            request.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); // Set content type

            // Create POST content
            String postData = "act=login&success_url=&fail_url=&try_to_login=1&to=&vk=&al_test=3&email=" + URLEncoder.encode(username, "UTF-8") + "&pass=" + URLEncoder.encode(password, "UTF-8") + "&expire=";
            request.setRequestProperty("Content-Length", Integer.toString(postData.getBytes().length)); // Set content length

            // Random stuffery
            request.setUseCaches(false);
            request.setDoInput(true);
            request.setDoOutput(true);

            // Send request
            DataOutputStream requestStream = new DataOutputStream(request.getOutputStream()); // Get output stream
            requestStream.writeBytes(postData); // Send post bytes
            Object obj = request.getContent(); // Needed to fetch cookies!

            requestStream.flush(); // Always flush afterwards :D
            requestStream.close(); // Close connection

            // Get cookies (hmmmm delicious cookies)
            CookieStore cookieJar = cookieManager.getCookieStore();
            List<HttpCookie> cookies = cookieJar.getCookies();

            // Loop through cookies
            for (HttpCookie cookie : cookies) {

                // Check for GUID
                if(cookie.getName().contains("remixsid")) {
                    Guid = cookie.getValue(); // Set GUID
                }

            }
        
        } catch(Exception e) {

            error = true; // An error occured
            
        } finally {

            if(request != null) {
                request.disconnect();
            }
            
        }

        if(Guid != null) { // Login went fine

            System.out.println("Login was successfull!");
            return true; // GUID id set login was a success

        } else if(error) { // An error occured
            
            System.out.println("Could not log in, an eror occured during the login procedure."); 

        } else { // User credentials invalid
            
            System.out.println("Could not log in, are the username and password valid?");

        }

		return false; // Default return

	}

    public void logout() {
        
        Guid = null; // Reset Guid

    }

	public List<Song> search(String query, int offset) {
		
		// TODO: Search
        return null;

	}

}